package com.undsf.chaldea.web.controllers

import com.undsf.chaldea.entities.Servant
import com.undsf.chaldea.web.entities.BasicMessage
import com.undsf.chaldea.web.services.ServantService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam

/**
 * Created by Arathi on 2017/8/23.
 */
@Controller
class NgServantController {
    @Autowired
    var servantSvc: ServantService? = null

    @RequestMapping("/servant/add/{metaId}")
    fun add(@RequestParam(value = "metaId") metaId: Int): BasicMessage {
        val masterId: Int = 1
        var msg: BasicMessage = BasicMessage(0, "成功")
        val servant: Servant? = servantSvc?.add(masterId, metaId)
        if (servant!!.id <= 0) {
            msg.code = 1
            msg.message = "添加失败"
        }
        return msg
    }

    @RequestMapping("/servant/levelup/{id}/{level}")
    fun levelup(@RequestParam(value = "id") id: Int, @RequestParam(value = "level") level: Int): BasicMessage {
        var msg: BasicMessage = BasicMessage(0, "成功")
        val result: Map<String, String> = servantSvc!!.levelUp(id, level)
        val resultCode = result["code"]?.toInt() ?: -1
        if (resultCode <= 0) {
            msg.code = resultCode
            msg.message = "升级失败"
        }
        return msg
    }

    @RequestMapping("/servant/skill/{servantId}/{skillId}/{level}")
    fun skillUpgrade(@RequestParam(value = "servantId") servantId: Int,
                     @RequestParam(value = "skillIndex") skillIndex: Int,
                     @RequestParam(value = "level") level: Int): BasicMessage {
        var msg: BasicMessage = BasicMessage(0, "成功")

        val result: Map<String, String> = servantSvc!!.skillUpgrade(servantId, skillIndex, level)
        val resultCode = result["code"]?.toInt() ?: -1

        if (resultCode <= 0) {
            msg.code = resultCode
            msg.message = "升级失败"
        }
        return msg
    }
}
